﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeartStone.Interface;

namespace HeartStone.BaseDomain
{
	public class BaseUnit : IUnitAction
	{
		public BaseUnit(BaseHero pHero)
		{
			OwnerHero = pHero;
			Name = "未知";
			Ac = 0;
			ActCount = 0;
			Ad = 0;
			BaseAd = 0;
			BaseHP = 0;
			this.HeroType = HeroType.None;
			this.Hp = 0;
			this.Shield = false;

		}

		public virtual void Dispose()
		{
			Console.WriteLine("{0}死亡", this.Name);
			return;
		}

		public BaseHero OwnerHero { get; set; }
		/// <summary>
		/// 职业类型
		/// </summary>
		public HeroType HeroType { get; set; }

		public string Name { get; set; }

		private int _Hp;
		/// <summary>
		/// 血量
		/// </summary>
		public int Hp
		{
			get { return _Hp; }
			set
			{
				//血量最高不得超过基础血量
				if (value > BaseHP)
					_Hp = BaseHP;
				else
					_Hp = value;
			}
		}
		/// <summary>
		/// 基础血量
		/// </summary>
		public int BaseHP { get; set; }

		private int _Ac { get; set; }
		/// <summary>
		/// 护甲
		/// </summary>
		public int Ac
		{
			get { return _Ac; }
			set
			{
				//护甲最多不能超过基础血量
				if (value > BaseHP)
					_Ac = BaseHP;
				else
					_Ac = value;
			}
		}

		/// <summary>
		/// 攻击
		/// </summary>
		public int Ad { get; set; }

		/// <summary>
		/// 基本攻击
		/// </summary>
		public int BaseAd { get; set; }

		/// <summary>
		/// 攻击次数，冲锋上场+1，风怒每回合+1
		/// </summary>
		public int ActCount { get; set; }

		private bool _Shield = false;

		/// <summary>
		/// 圣盾
		/// </summary>
		public bool Shield
		{
			get { return _Shield; }
			set { _Shield = value; }
		}

		public virtual void Attack(BaseUnit targeted, BaseUnit from, int ad)
		{
			targeted.Defense(targeted, from, ad);
		}

		public virtual void Defense(BaseUnit targeted, BaseUnit from, int ad)
		{
			if (Shield)
				Shield = false;
			else
			{
				int acDefend = 0;
				if (targeted.Ac > 0)
				{
					acDefend = targeted.Ac - ad;

					if (acDefend >= 0)
						targeted.Ac -= ad;	//攻击5，护甲10，10-5大于等于0,表示护甲比攻击高，只要减护甲即可
					else
					{
						//攻击5，护甲2,2-5=-3,小于0，表示护甲不够，要减
						targeted.Ac = 0;
						targeted.Hp += acDefend;
					}
				}
				else
					targeted.Hp -= ad;
				Console.WriteLine("{0}受到{1}的{2}伤害，当前护甲{3}，生命{4}", targeted.Name, from.Name, ad, targeted.Ac, targeted.Hp);
				from.MakeDamage(targeted, from, ad);
			}
		}



		/// <summary>
		/// 执行治疗
		/// </summary>
		/// <param name="targeted"></param>
		/// <param name="from"></param>
		/// <param name="ad"></param>
		public void Treat(BaseUnit targeted, BaseUnit from, int ad)
		{
			var hero = from as BaseHero;
			bool flag = true;
			if (hero != null)
				flag = hero.treatFlag;
			else
				flag = from.OwnerHero.treatFlag;


			if (flag)
			{
				targeted.Hp += ad;
				Console.WriteLine("{0}受到{1}的{2}治疗", targeted.Name, from.Name, ad);
			}
			else
			{
				if (targeted.Hp >= 0)
				{
					//from制造了伤害
					Defense(targeted, from, ad);
				}
			}
		}


		public virtual void MakeDamage(BaseUnit targeted, BaseUnit from, int ad)
		{
			return;
		}
	}
}
